package view;


import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import controller.ControllerHome;
import controller.ControllerViewShapes;

public class ViewShapes extends JPanel {

	private static final long serialVersionUID = 1L;
	private ControllerViewShapes thisController;
	
	public ViewShapes(ControllerViewShapes controller) {

        thisController = controller;
        thisController.setView(this);

        setPreferredSize(new Dimension(950, 450));
        setBackground(Color.WHITE);
        
        RefreshShapes();

        addMouseListener(thisController);
        
	}
	
	public void RefreshShapes() {

		repaint();

	}
	
	public void paintComponent(Graphics g) {

		super.paintComponent(g);

		Graphics2D g1 = (Graphics2D) g;

		for(int i = (ControllerHome.image.getNumberShapes() - 1); i > 0; i--) {

			boolean shapeHasBorder = ControllerHome.image.getShape(i).getBorderSize() > 0;
			boolean shapeIsIronWire = ControllerHome.image.getShape(i).getIsIronWire() == true;

			if(ControllerHome.image.getShape(i).getClass().toString().contains("MyRectangle")) {

				if (shapeIsIronWire){
					
					int posX = ControllerHome.image.getShape(i).getPos().getX() - 1;
					int posY = ControllerHome.image.getShape(i).getPos().getY() - 1;
					int width = ControllerHome.image.getShape(i).getWidth() + 1;
					int height = ControllerHome.image.getShape(i).getHeight() + 1;
					
					BasicStroke border = new BasicStroke(ControllerHome.image.getShape(i).getBorderSize(), BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER);
					
					g1.setStroke(border);
					
					g1.setPaint((Color) ControllerHome.image.getShape(i).getType().getOutline());
					
					g1.draw(new Rectangle2D.Double(posX, posY, width, height));
					
				}
				else if(shapeHasBorder) {
					
					int posX = ControllerHome.image.getShape(i).getPos().getX() - 1;
					int posY = ControllerHome.image.getShape(i).getPos().getY() - 1;
					int width = ControllerHome.image.getShape(i).getWidth() + 1;
					int height = ControllerHome.image.getShape(i).getHeight() + 1;
					
					BasicStroke border = new BasicStroke(ControllerHome.image.getShape(i).getBorderSize(), BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER);
					
					g1.setStroke(border);

					g1.setPaint((Color) ControllerHome.image.getShape(i).getType().getOutline());
					
					g1.draw(new Rectangle2D.Double(posX, posY, width, height));
					
					g1.setPaint((Color) ControllerHome.image.getShape(i).getType().getFilling());
					
					g1.fill(new Rectangle2D.Double(posX, posY, width, height));
					
					
				}
				else {
					
					int posX = ControllerHome.image.getShape(i).getPos().getX();
					int posY = ControllerHome.image.getShape(i).getPos().getY();
					int width = ControllerHome.image.getShape(i).getWidth();
					int height = ControllerHome.image.getShape(i).getHeight();
					
					g1.setPaint((Color) ControllerHome.image.getShape(i).getType().getFilling());
					
					g1.fill(new Rectangle2D.Double(posX, posY, width, height));
					
				}
				
			}
			else if(ControllerHome.image.getShape(i).getClass().toString().contains("MyEllipse")) {

				
				
			}
			
		}
		
		this.repaint();
		
	}

}
